
$('#overtime').datepicker({
    format: 'yyyy-mm-dd'
});
function findTaskList(certusUtils1){
    certusUtils1.ajax("findMemberList",{"proId":proId},function (response) {
        var data = response.data;
        for (var i=0;i<data.length;i++){
            $("#fzr").append("<option value='"+data[i].userId+"'>"+data[i].userName+"</option>");
        }
    });

    certusUtils1.ajax("findTaskTreeList",{"proId":proId}, function(result){
        zNodesArr = result.data;
        $.fn.zTree.init($("#treeDemo"), setting, zNodesArr);
    });

    certusUtils1.ajax("findTargets",{"proId":proId}, function(result){
        var data = result.data;
        for (var i=0;i<data.length;i++){
            $("#proTarget").append("<option value='"+data[i].id+"'>"+data[i].proTarget+"</option>");
        }
    });
}
    var setting = {
        view: {
            showIcon: false
        },
        edit: {
            drag: {
                autoExpandTrigger: true,
                prev: dropPrev,
                inner: dropInner,
                next: dropNext
            },
            enable: true,
            showRemoveBtn: false,
            showRenameBtn: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeDrop: beforeDrop,
            beforeDragOpen: beforeDragOpen,
            onDrag: onDrag,
            onDrop: onDrop,
            onExpand: onExpand,
            beforeClick: beforeClick,
            onClick: onClick
        }
    };

    $("#callbackTrigger").bind("change", {}, setTrigger);
    $("#addLeaf").bind("click", {
        isParent: false
    }, add1);
    $("#remove").bind("click", remove);
    $("#edit").bind("click", edit);
    var newCount = 1;

    function add1(e) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            isParent = e.data.isParent,
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        if (treeNode==null||treeNode==undefined){
            layer.msg("请先选择一个任务节点再进行新增",{ time:1000});
            return;
        }
        var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
        var ctLength=0;
        if (treeNode.children!=undefined&&treeNode.children!="undefined"){
            ctLength = treeNode.children.length;
        }
        if (sNodes.length > 0) {
            var node = sNodes[0].getParentNode();
            if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
                layer.msg("请先选择一个任务节点再进行新增",{ time:1000});
                return;
            }
            if (sNodes[0].childOuter == false) {
                var val = $("#penName").val();
                var chilName = $("#chilName").val();
                var krqz = $("#krqz").val();
                var fzr = $("#fzr").val();
                var overtime = $("#overtime").val();
                var okrt = $("#okrt").val();
                if (chilName == null || chilName == "" || chilName == undefined) {
                    layer.msg("子任务不能为空！",{ time:1000});
                    return;
                }
                if (krqz == null || krqz == "" || krqz == undefined) {
                    layer.msg("KR权重不能为空！",{ time:1000});
                    return;
                }
                if (fzr == null || fzr == "" || fzr == undefined) {
                    layer.msg("负责人不能为空！",{ time:1000});
                    return;
                }
                if (overtime == null || overtime == "" || overtime == undefined) {
                    layer.msg("完成时间不能为空！",{ time:1000});
                    return;
                }
                if (okrt == null || okrt == "" || okrt == undefined) {
                    layer.msg("投入百分比不能为空！",{ time:1000});
                    return;
                } else {
                    var list ={};
                    list.krsDesc = chilName;
                    list.krsWeight = krqz;
                    list.fzUser = fzr;
                    list.findshTime = overtime;
                    list.inputWeight = okrt;
                    list.sort = ctLength+1;
                    list.oId = sNodes[0].pId;
                    list.tId = sNodes[0].id;
                    list.tdesc = sNodes[0].name;
                    list.odesc = node.name;
                    list.targetId = $("#proTarget").val();

                    treeNode = zTree.addNodes(treeNode, {
                        id: (100 + newCount),
                        pId: treeNode.id,
                        isParent: isParent,
                        name: chilName
                    });
                    $certusUtils.ajax("addOkrTask",{
                        "list":JSON.stringify(list),
                        "proId":proId,
                        "isAdd":"Y",
                        "pid":0,
                        "uKrsId":""
                    }, function(result){
                        layer.msg("新增成功",{ time:500});
                        toLoadHtml(12);
                    });
                }
            } else {
                layer.msg("请先选择一个任务节点再进行新增",{ time:500});
                return;
            }
        }
    }


    function edit(e) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            isParent = false,
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        if (treeNode==null||treeNode==undefined){
            layer.msg("请先选择一个任务节点再进行新增",{ time:1000});
            return;
        }
        var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
        if (sNodes.length > 0) {
            var node = sNodes[0].getParentNode();
            if (sNodes[0].childOuter == false) {
                layer.msg("请先选择一个子任务节点再进行更新",{ time:1000});
                return;
            } else {
                var val = $("#chilName").val();
                var krqz = $("#krqz").val();
                var fzr = $("#fzr").val();
                var overtime = $("#overtime").val();
                var okrt = $("#okrt").val();
                if (val == null || val == "" || val == undefined) {
                    layer.msg("子任务不能为空！",{ time:1000});
                    return;
                }
                if (krqz == null || krqz == "" || krqz == undefined) {
                    layer.msg("KR权重不能为空！",{ time:1000});
                    return;
                }
                if (fzr == null || fzr == "" || fzr == undefined) {
                    layer.msg("负责人不能为空！",{ time:1000});
                    return;
                }
                if (overtime == null || overtime == "" || overtime == undefined) {
                    layer.msg("完成时间不能为空！",{ time:1000});
                    return;
                }
                if (okrt == null || okrt == "" || okrt == undefined) {
                    layer.msg("投入百分比不能为空！",{ time:1000});
                    return;
                } else {
                    var list ={};
                    list.krsDesc = val;
                    list.krsWeight = krqz;
                    list.fzUser = fzr;
                    list.findshTime = overtime;
                    list.inputWeight = okrt;
                    list.oSort = 0;
                    list.targetId = $("#proTarget").val();
                    treeNode.name = val;
                    var id = sNodes[0].id;

                    treeNode.name = val;
                    treeNode = zTree.updateNode(treeNode);
                    $certusUtils.ajax("addOkrTask",{
                        "list":JSON.stringify(list),
                        "uKrsId":id,
                        "isAdd":"N",
                        "proId":proId,
                        "pid":sNodes[0].pId
                    }, function(result){
                        layer.msg("更新成功",{ time:500});
                    });
                }
            }
        }
    }

    function remove(e) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
        var index = zTree.getNodeIndex(sNodes[0])+1;
        if (sNodes.length > 0) {
            var id = sNodes[0].id;
            var node = sNodes[0].getParentNode();
            if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
                layer.msg("请先选择一个子任务节点再进行删除",{ time:500});
                return;
            }
            if (sNodes[0].childOuter == false) {
                layer.msg("请先选择一个子任务节点再进行删除",{ time:500});
                return;
            } else {
                var callbackFlag = $("#callbackTrigger").attr("checked");
                layer.confirm('确认删除子任务节点？', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    // mmg.updateRow(item1, 0);
                    $certusUtils.ajax("deleteTask",{
                        "krsId":id,"index":index
                    }, function(result){
                        var data = result.data;
                        if(data==true){
                            zTree.removeNode(treeNode, callbackFlag);
                            $("#chilName").val("");
                            $("#krqz").val("");
                            $("#fzr").val("");
                            $("#autocomplete").val("");
                            $("#overtime").val("");
                            $("#okrt").val("");
                            layer.msg('删除成功', {
                                icon: 1
                            });
                        }else{
                            layer.msg('当前子任务存有进展和得分，不能删除');
                        }
                    });


                });
            }
        }
    }

    function onClick(event, treeId, treeNode, clickFlag) {
        var sNodes = $.fn.zTree.getZTreeObj("treeDemo").getSelectedNodes();
        if(sNodes[0].level==2){
            var id = sNodes[0].id;
            $certusUtils.ajax("getTaskXx",{"krsId":id,"projId":proId,"pid":sNodes[0].pId}, function(result){
                var data = result.data;
                $("#krqz").val(data.kr_weight);
                $("#fzr").val(data.task_fzuser_id);
                $("#autocomplete").val(data.user_name+"["+data.positon_name+"]");
                $("#overtime").val(data.time);
                $("#okrt").val(data.input_weight);
            });
        };
        var penNode, penName, chilName;
        if (sNodes.length > 0) {
            var node = sNodes[0].getParentNode();
            // console.log(node);
            if (sNodes[0].pId == null || sNodes[0].pId == "" || sNodes[0].pId == undefined) {
                layer.msg("请选择一个任务节点或子任务节点",{ time:500});
                return;
            }
            if (sNodes[0].pId != null || sNodes[0].pId != "" || sNodes[0].pId != undefined) {
                penNode = node.getParentNode();
                if (penNode != null) {
                    penNode = penNode.name;
                }
                penName = node.name;
                chilName = sNodes[0].name;
                showLog(penNode, penName, chilName)
            }
        }
    }



function dropPrev(treeId, nodes, targetNode) {
    var pNode = targetNode.getParentNode();
    if (pNode && pNode.dropInner === false) {
        return false;
    } else {
        for (var i = 0, l = curDragNodes.length; i < l; i++) {
            var curPNode = curDragNodes[i].getParentNode();
            if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                return false;
            }
        }
    }
    return true;
}

function dropInner(treeId, nodes, targetNode) {
    if (targetNode && targetNode.dropInner === false) {
        return false;
    } else {
        for (var i = 0, l = curDragNodes.length; i < l; i++) {
            if (!targetNode && curDragNodes[i].dropRoot === false) {
                return false;
            } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
                return false;
            }
        }
    }
    return true;
}

function dropNext(treeId, nodes, targetNode) {
    var pNode = targetNode.getParentNode();
    if (pNode && pNode.dropInner === false) {
        return false;
    } else {
        for (var i = 0, l = curDragNodes.length; i < l; i++) {
            var curPNode = curDragNodes[i].getParentNode();
            if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                return false;
            }
        }
    }
    return true;
}

var log, className = "dark",
    curDragNodes, autoExpandNode;

function beforeDrag(treeId, treeNodes) {
    className = (className === "dark" ? "" : "dark");
    // showLog("[ " + getTime() + " beforeDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes.");
    for (var i = 0, l = treeNodes.length; i < l; i++) {
        if (treeNodes[i].drag === false) {
            curDragNodes = null;
            return false;
        } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
            curDragNodes = null;
            return false;
        }
    }
    curDragNodes = treeNodes;
    return true;
}

function beforeDragOpen(treeId, treeNode) {
    autoExpandNode = treeNode;
    return true;
}

function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
    className = (className === "dark" ? "" : "dark");
    // showLog("[ " + getTime() + " beforeDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
    // showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is " + (isCopy == null ? "cancel" : isCopy ? "copy" : "move"));
    return true;
}

function onDrag(event, treeId, treeNodes) {
    className = (className === "dark" ? "" : "dark");
    // showLog("[ " + getTime() + " onDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes.");
}

function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
    className = (className === "dark" ? "" : "dark");
    // showLog("[ " + getTime() + " onDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
    // showLog("target: " + (targetNode ? targetNode.name : "root") + "  -- is " + (isCopy == null ? "cancel" : isCopy ? "copy" : "move"))
}

function onExpand(event, treeId, treeNode) {
    if (treeNode === autoExpandNode) {
        className = (className === "dark" ? "" : "dark");
        showLog("[ " + getTime() + " onExpand ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name);
    }
}
var log, className = "dark";

function beforeClick(treeId, treeNode, clickFlag) {
    className = (className === "dark" ? "" : "dark");
    showLog(treeNode.name);
    return (treeNode.click != false);
}

function showLog(penNode, penName, chilName) {
    if (penNode != null) {
        $("#penNode").val(penNode);
        $("#penName").val(penName);
        $("#chilName").val(chilName);
    } else {
        $("#penNode").val(penName);
        $("#penName").val(chilName);
    }
}

function getTime() {
    var now = new Date(),
        h = now.getHours(),
        m = now.getMinutes(),
        s = now.getSeconds(),
        ms = now.getMilliseconds();
    return (h + ":" + m + ":" + s + " " + ms);
}

function setTrigger() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
}
function back(){
    toLoadHtml(4,4);
}